package com.subaiqiao.yunshiyuan.entrance.ysy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subaiqiao.yunshiyuan.basis.utils.Result;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.ProductCategory;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.ProductCategoryService;
import com.subaiqiao.yunshiyuan.entrance.ysy.vo.ProductCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 曹朝禹
 * @since 2022-05-13
 */
@RestController
@RequestMapping("/ysy/product-category")
@Api(tags = "商品类别信息")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @ApiOperation("查询所有商品类型信息")
    @GetMapping("/FindProductCategoryPage.do")
    public Result FindProductCategoryPage(
            @RequestParam @ApiParam("页码") Integer pageNum,
            @RequestParam @ApiParam("数量") Integer pageSize,
            @RequestParam(defaultValue = "") @ApiParam("类别名称") String productCategoryName,
            @RequestParam(defaultValue = "") @ApiParam("父类类别名称") String productCategoryParentName
    ){
        QueryWrapper<ProductCategoryVO> wrapper = new QueryWrapper<>();
        if(!productCategoryName.equals("")){
            wrapper.eq("product_category.product_category_name", productCategoryName);
        }
        wrapper.orderByDesc("product_category.product_category_sort");
        if(!productCategoryParentName.equals("")){
            QueryWrapper<ProductCategory> wrapperTemp = new QueryWrapper<>();
            wrapperTemp.eq("product_category_name", productCategoryParentName);
            Integer productCategoryParentId = productCategoryService.getOne(wrapperTemp).getProductCategoryId();
            wrapper.eq("product_category.product_category_parent_id", productCategoryParentId);
        }
        Page<ProductCategoryVO> page = new Page<>(pageNum, pageSize);
        return Result.success(productCategoryService.getProductCategoryVO(page, wrapper));
    }

    @ApiOperation("查询所有类型")
    @GetMapping("/FindProductCategory.do")
    public Result FindProductCategory(){
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("product_category_sort");
        return Result.success(productCategoryService.list(wrapper));
    }

    @ApiOperation("查询所有父级类型")
    @GetMapping("/FindProductCategoryParent.do")
    public Result FindProductCategoryParent(){
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.isNull("product_category_parent_id");
        return Result.success(productCategoryService.list(wrapper));
    }

    @ApiOperation("添加商品类型")
    @PostMapping("/InsertProductCategory.do")
    public Result InsertProductCategory(ProductCategory entity){
        boolean result = false;
        if(entity.getProductCategoryId()==null) result = productCategoryService.save(entity);
        else result = productCategoryService.updateById(entity);
        if(result) return Result.success();
        return Result.error();
    }

    @ApiOperation("查询类别详情")
    @GetMapping("/FindProductCategoryDetail.do")
    public Result FindProductCategoryDetail(@RequestParam @ApiParam("商品类型编号") Integer productCategoryId){
        return Result.success(productCategoryService.getById(productCategoryId));
    }

    @ApiOperation("删除一个类别")
    @DeleteMapping("/DeleteProductCategory.do")
    public Result DeleteProductCategory(@RequestParam @ApiParam("商品类型编号") Integer productCategoryId){
        try {
            return Result.success(productCategoryService.removeById(productCategoryId));
        }catch (DataAccessException e){
            return Result.error("当前存在子类型或存在商品");
        }
    }


    @ApiOperation("查询商品类型总数")
    @GetMapping("/FindProductCategoryCount.do")
    public Result FindProductCategoryCount(){
        return Result.success(productCategoryService.FindProductCategoryCount());
    }

    @ApiOperation("查询父类商品类型总数")
    @GetMapping("/FindProductCategoryParentCount.do")
    public Result FindProductCategoryParentCount(){
        return Result.success(productCategoryService.FindProductCategoryParentCount());
    }

}
